package com.example.pharmacypick.controller;


import com.example.pharmacypick.dto.DrugDto;
import com.example.pharmacypick.pojo.Drug;
import com.example.pharmacypick.service.DrugService;
import com.example.pharmacypick.utils.RespBean;
import com.example.pharmacypick.vo.DataGridView;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/drug")
public class DrugController {

    @Autowired
    DrugService drugService;

    /**
     *
     * @param drug_name 查询参数
     * @param pageNum  当前页数
     * @param pageSize 页面大小
     * @return 返回封装的对象
     */
    @GetMapping
    public RespBean findAllDrugs(@RequestParam String drug_name,@RequestParam String drug_factory,  @RequestParam Integer pageNum,@RequestParam Integer pageSize){
        try {
            //设置传来的参数
            DrugDto drugDto=new DrugDto();
            //只通过名字来查询
            drugDto.setDrugName(drug_name);
            drugDto.setDrugFactory(drug_factory);
            drugDto.setPageNum(pageNum);
            drugDto.setPageSize(pageSize);
            DataGridView allDrugs = drugService.findAllDrugs(drugDto);
            return RespBean.success("成功",allDrugs);
        }catch (Exception e){
            e.printStackTrace();
            return RespBean.error("error");
        }
    }

    @PostMapping
    public RespBean addDrug(@RequestBody DrugDto drugDto){
        try {
            drugService.addDrug(drugDto);
            return RespBean.success("成功");
        }catch (Exception e){
            e.printStackTrace();
            return RespBean.error("error");
        }
    }

    //更新
    @PutMapping
    public RespBean updateDrug(@RequestBody DrugDto drugDto){
        try {
            drugService.updateDrug(drugDto);
            return RespBean.success("成功");
        }catch (Exception e){
            e.printStackTrace();
            return RespBean.error("error");
        }
    }

    @DeleteMapping("/{drugNum}")
    public RespBean deleteDrug(@PathVariable Long drugNum){
        try {
            drugService.deleteDrug(drugNum);
            return RespBean.success("成功");
        }catch (Exception e){
            e.printStackTrace();
            return RespBean.error("error");
        }
    }

    /**
     * 根据处方号来更新库存
     * @param prescriptionId 处方号
     * @return 返回更新的结果
     */
    @PutMapping("updateStock/{prescriptionId}")
    public RespBean updateStock(@PathVariable int prescriptionId){
        try {
            drugService.updateDrugStock(prescriptionId);
            return RespBean.success("成功");
        }catch (Exception e){
            e.printStackTrace();
            return RespBean.error("error");
        }
    }

}
